<?php
declare (strict_types = 1);

namespace app\course\service\offline\admin;

use app\common\logic\Attach;
use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\course\logic\offline\admin\Offline as OfflineLogic;
use app\school\model\school\School as SchoolModel;
use app\user\logic\teacher\admin\Teacher as TeacherLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 面授课程接口类-后台
 */
class Offline extends BaseService
{
    /**
     * 列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-23
     * @return   [type]                         [description]
     */
    function list() {
        // 搜索功能
        $map = [];
        // Request::has('category', 'get', true) && $map['category']      = ['like', '%,' . Request::get('category/s') . ',%'];
        Request::has('title', 'get', true) && $map['title'] = Request::get('title/s');
        // if ( Request::has('price_min', 'get') && Request::has('price_max', 'get') ) {
        //     $map['price'] =  ['BETWEEN', Request::get('price_min/d'), Request::get('price_max/d')];
        // } elseif ( Request::has('price_min', 'get', true) ) {
        //     $map['price'] =  ['>=', Request::get('price_min/d')];
        // } elseif ( Request::has('price_max', 'get', true) ) {
        //     $map['price'] =  ['<=', Request::get('price_max/d')];
        // }
        $data = app(OfflineLogic::class)->offlineList($map);
        // 定义字段
        $keyList = [
            'id'           => ListItem::make()->title('ID'),
            'title'        => ListItem::make()->title('名称'),
            'cover'        => ListItem::make()->title('封面'),
            'price'        => ListItem::make()->title('价格'),
            'school_name'  => ListItem::make()->title('机构'),
            'teacher_name' => ListItem::make()->title('讲师'),
            'status'       => ListItem::make('custom')->title('状态'),
            // 'update_status' => ListItem::make()->title('连载状态'),
        ];
        $searchKey = [
            'title'  => FormItem::make('input')->title(false)->placeholder('名称')->maxlength(30),
            'mhm_id' => FormItem::make('input')->title(false)->placeholder('机构')->maxlength(30),
            'status' => FormItem::make('select')->title(false)->placeholder('状态')->options([
                ['title' => '不限制', 'value' => -1],
                ['title' => '正常', 'value' => 1],
                ['title' => '禁止', 'value' => 0],
            ]),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }
    /**
     * 创建课程
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-15
     * @param    Request                        $request [description]
     * @return   [type]                                  [description]
     */
    public function create()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $re   = app(OfflineLogic::class)->addOffline($data);
            return $this->parseToData($re->toArray(), 1, '添加成功');
        }
        /* 创建课程界面配置 */
        $keyList = $this->keyListInit();
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;
        // 排序
        // $form->pageKeySort = ['mhm_id'];
        $form->saveMethod  = 'POST';
        $url               = 'course/admin/offline/create';
        $form->savePostUrl = $url;

        return $form->send();

    }

    /**
     * 修改课程信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-15
     */
    public function update($id)
    {
        if (Request::isPut()) {
            $data    = Request::put();
            $offline = OfflineLogic::where('id', $id)->findOrFail();
            $offline = app(OfflineLogic::class)->editOffline($offline, $data);
            return $this->parseToData($offline->toArray(), 1, '编辑成功');
        }

        $data           = app(OfflineLogic::class)->findOrFail($id);
        $data->category = array_map(function ($v) {return $v * 1;}, explode(',', trim($data->fullcategorypath, ',')));
        $attach           = Attach::find($data['cover']);
        $data->cover_list = [
            [
                'id'   => $attach ? $attach->id : 0,
                'name' => $attach ? $attach->filename : '',
                'url'  => $attach ? $attach->fileurl : '',
            ],
        ];
        // 创建课程界面配置
        $keyList = $this->keyListInit();
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->datas       = $data;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = 'course/admin/offline/' . $id;
        return $form->send();
    }

    /**
     * 课程界面配置
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-26
     */
    public function keyListInit()
    {
        // 机构信息
        $school = app(SchoolModel::class)->scope('status')->field('id as value,title')->select()->toArray();
        // 讲师信息
        $teacher = app(TeacherLogic::class)->getSelectOptions();
        // 课程分类
        $category = app(Category::class)->cascaderCate();
        // 课程层级
        $section_level = [
            ['title' => '一级', 'value' => 1],
            ['title' => '二级', 'value' => 2],
            ['title' => '三级', 'value' => 3],
        ];
        // 定义字段
        $keyList = [
            'mhm_id'         => FormItem::make('select')->title('机构')->options($school)->required(),
            'teacher_id'     => FormItem::make('select')->title('讲师')->options($teacher)->required(),
            'cates'          => FormItem::make('cascader')->title('分类')->options($category)->multiple(true),
            'title'          => FormItem::make()->title('课程名称')->maxlength(100)->required(),
            'cover'          => FormItem::make('custom')->title('课程封面')->limit(1)->required(),
            'scribing_price' => FormItem::make('inputNumber')->title('原价')->min(0)->precision(2),
            'price'          => FormItem::make('inputNumber')->title('售价')->min(0)->precision(2)->required(),
            'vip_price'      => FormItem::make('inputNumber')->title('会员价')->min(0)->precision(2),
            'give_credit'    => FormItem::make('inputNumber')->title('购买赠送积分')->min(0),
            'section_level'  => FormItem::make('select')->title('目录层级')->help('确定课程目录结构，如三级【章-节-课】')->options($section_level)->required(),
            'info'           => FormItem::make('editor')->title('简介'),
            'listing_time'   => FormItem::make('date')->title('上架时间')->dateType(FormItem::option('DateType', 'DATETIME'))->format('yyyy-MM-dd HH:mm')->required(),
        ];

        return $keyList;
    }

    /**
     * 编辑指定的字段
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-27
     */
    public function editField($id)
    {
        $data  = Request::put();
        $video = app(OfflineLogic::class)->editField($id, $data);
        return $this->parseToData($video->toArray(), 1, '编辑成功');
    }

    /**
     * 删除课程
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-15
     */
    public function delete($id)
    {
        $res = app(OfflineLogic::class)->deleteOffline($id);

        return $this->parseToData($res, 1, '删除成功');

    }
}
